<template>
  <div class="login">
    <el-card class="box-card loginCard">
      <div slot="header" class="clearfix">
        <span>乐淘商城后台管理系统</span>
      </div>
      <div>
        <el-form
          :model="ruleForm2"
          status-icon
          :rules="rules2"
          ref="ruleForm2"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="pass">
            <el-input v-model="ruleForm2.pass" autocomplete="off" type="username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="checkPass">
            <el-input v-model="ruleForm2.checkPass" autocomplete="off" type="password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loginForm">登录</el-button>
            <el-button @click="resetForm('ruleForm2')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
<script>
import { login } from '@/api'
export default {

  data () {
    return {
      ruleForm2: {
        pass: 'root',
        checkPass: '123456'
      },
      rules2: {
        pass: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginForm () {
      this.$refs.ruleForm2.validate(isOk => {
        if (isOk) {
          login({ username: this.ruleForm2.pass, password: this.ruleForm2.checkPass })
            .then(res => {
              console.log(res)
              if (res.data.success) {
                this.$message.success('登陆成功')
                this.$router.push({ name: 'home' })
              } else {
                this.$message.error(res.data.message)
              }
            })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}

</script>

<style lang="scss" scoped>
.login {
  display: block;
  .loginCard {
    margin: 200px auto;
    width: 450px;
    .text {
      font-size: 14px;
    }

    .item {
      margin-bottom: 18px;
    }

    .clearfix:before,
    .clearfix:after {
      display: table;
      content: '';
    }
    .clearfix:after {
      clear: both;
    }

    .box-card {
      width: 480px;
    }
  }
}
</style>
